﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Commonlib;
namespace AlbonWeb.Order
{
    public partial class ItemCloth : System.Web.UI.Page
    {
        private readonly BLL.ClothBLL clothbll = new BLL.ClothBLL();
        private readonly BLL.ItemClothBLL itemclothbll = new BLL.ItemClothBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request["orderitemid"] != null)
                {
                    BLL.ClothCompanyBLL clothcompanybll = new BLL.ClothCompanyBLL();
                    bindDDL(ddlCoName, "shortName", "ID",clothcompanybll.getActiveCompany());
                    ddlCoName.Items.Insert(0,new ListItem("-Cloth Company-","0"));
                    //绑定布料信息
                    bindItemCloth();
                    //绑定数量和试身信息
                    BLL.OrderItemBLL orderitembll = new BLL.OrderItemBLL();
                    DataTable dt=orderitembll.getItemByID(Request["orderitemid"].ToString());
                    if (dt.Rows[0]["quantity"].ToString() != "")
                    {
                        ddlQty.SelectedValue = dt.Rows[0]["quantity"].ToString();    
                    }
                    if (dt.Rows[0]["FT"].ToString() != "")
                    {
                        isFT.Checked = Boolean.Parse(dt.Rows[0]["FT"].ToString());
                    }
                    //客户不能修改布料尺寸(其它类型的用户可以修改)
                    BLL.UserBLL userbll=new BLL.UserBLL();
                    if (userbll.isDealer)
                    {
                        clothSize.Enabled = false;
                    }
                }
            }
        }
        protected void bindItemCloth()
        { 
            string cmdstr = "select * from ItemClothView where active=1 and orderItemID="+Request["orderitemid"].ToString();
            SqlHelper.BindRepeater(repItemCloth, SqlHelper.SqlConnectionString, CommandType.Text, cmdstr, null);
        }
        protected void bindDDL(DropDownList ddl, string textField, string valueField,DataTable source)
        {
            ddl.DataSource = source;
            ddl.DataTextField = textField;
            ddl.DataValueField = valueField;
            ddl.DataBind();
        }
        
        //选择布料布料公司后加载布料号码
        protected void ddlCoName_TextChanged(object sender, EventArgs e)
        {
            string companyid = ddlCoName.SelectedValue;
            bindDDL(ddlClothNo, "No_Price", "clothNo", clothbll.getClothViewByCompanyID(companyid));
            ddlClothNo.Items.Insert(0, "--select cloth--");
        }

        //选择布料号码后显示布料详细信息
        protected void ddlClothNo_TextChanged(object sender, EventArgs e)
        {
           showDetail(ddlCoName.SelectedValue, ddlClothNo.SelectedValue);
        }

        protected void showDetail(string companyid,string clothNo)
        {
            
            DataTable dt = clothbll.getClothViewByNo(companyid, clothNo);
            lbWidth.Text = dt.Rows[0]["widthKind"].ToString();
            lbPrice.Text = dt.Rows[0]["clothPrice"].ToString();
            lbColor.Text = dt.Rows[0]["clothColor"].ToString();
            lbQuality.Text = dt.Rows[0]["clothQuality"].ToString();
        }
        //选择布料
        protected void selectBtn_Click(object sender, EventArgs e)
        {
            lbmessage.Text = "";
            if (ddlCoName.SelectedIndex == 0)
            {
                lbmessage.Text = "Cloth Comany is not selected!";
            }
            else if (ddlClothNo.SelectedIndex == 0)
            {
                lbmessage.Text = "Cloth No is not selected!";
            }
            else
            {
                Albon.Model.ItemCloth itemcloth = new Albon.Model.ItemCloth();
                itemcloth.ID = int.Parse(clothid.Value);
                itemcloth.companyName = ddlCoName.SelectedItem.Text;
                itemcloth.clothNo = ddlClothNo.SelectedValue;
                itemcloth.widthKind = lbWidth.Text;
                itemcloth.clothSize = decimal.Parse(clothSize.Text.Trim());
                string[] clothPrice = lbPrice.Text.Split('/');
                itemcloth.clothPrice = float.Parse(clothPrice[0]);
                itemclothbll.selectCloth(itemcloth);
                bindItemCloth();
                UpdatePanel1.Visible = false;
            }
        }
        //点击修改按钮后加载布料信息
        protected void imgbtn_Click(object sender, ImageClickEventArgs e)
        {
            UpdatePanel1.Visible = true;
            lbmessage.Text = "";
            ImageButton imgbtn = (ImageButton)sender;
            string id = imgbtn.ToolTip;
            DataTable dt = itemclothbll.getItemClothByID(id);
            styleName.Text = dt.Rows[0]["styleName"].ToString();
            clothSize.Text = dt.Rows[0]["clothSize"].ToString();
            clothid.Value = id;
            //如果已选择布料，则加载布料信息
            if (dt.Rows[0]["companyName"].ToString() != "")
            {
                try
                {
                    for (int i = 0; i < ddlCoName.Items.Count; i++)
                    {
                        if (ddlCoName.Items[i].Text == dt.Rows[0]["companyName"].ToString())
                        {
                            ddlCoName.SelectedIndex = i;
                            break;
                        }
                    }
                    bindDDL(ddlClothNo, "No_Price", "clothNo", clothbll.getClothViewByCompanyName(dt.Rows[0]["companyName"].ToString()));
                    ddlClothNo.Items.Insert(0, "--select cloth--");
                    ddlClothNo.SelectedValue = dt.Rows[0]["clothNo"].ToString();
                    showDetail(ddlCoName.SelectedValue, ddlClothNo.SelectedValue);
                }
                catch
                {
                    lbmessage.Text = "Can't load selected cloth info!!";
                }
            }
        }

        //将数量和试身信息添加到临时表中,并将订单项标记为已完成 
        protected void btnNextA_Click(object sender, EventArgs e)
        {
            BLL.OrderItemBLL orderitembll = new BLL.OrderItemBLL();
            Albon.Model.OrderItem orderitem = new Albon.Model.OrderItem();
            orderitem.ID=int.Parse(Request["orderitemid"].ToString());
            orderitem.quantity =int.Parse(ddlQty.SelectedItem.Text);
            orderitem.FT = isFT.Checked;
            orderitem.Finished = true;
            orderitembll.updateOrderItem(orderitem);
            //判断是否已经选择布料
            if (orderitembll.clothSelected(Request["orderitemid"].ToString()))
            {
                Response.Redirect("/Order/OrderItemList.aspx?orderid=" + Session["orderid"].ToString());
            }
            else {
                UpdatePanel1.Visible = true;
                lbmessage.Text = "Pls finish selecting the cloth";
            }
        }

    }
}